<?php

require_once("inc.admin.php");



$this_title="$vars[admin_title] &raquo; ".__("Member Incentive");

$page_title=__("Member Incentive");



if(!$pv["task"]["Withdrawal"]){

	$errmsg="<h2>$page_title</h2>".format_err(__("You do not have the privilege to manage member withdrawal."));



	print format_admin_page($errmsg, $this_title);

	exit();

}





$item_per_page=$vars['admin_item_per_page'];

//#####SEARCH POST#####

if($_GET["s"]){

	$searching=1;

	$search_query=wit_search($get_d);

}

//textbox search, prefix "_sp_"

$r_textsearch=array("id"=>__("Incentive ID"),"uid"=>__("Member ID"));

$search_options["status"]["option"] = $r_status = array("pending","paid","cancelled");

$search_options["status"]["option_display"] = $r_status_d = array("pending"=>__("Pending Payment"),"paid"=>__("Paid"),"cancelled"=>__("Cancelled"));

$search_options["status"]["display"] = __("Status");

$searchfields_ordering=array("id","uid");

$search_ui=build_admin_search_ui($searching, $this_file, $search_options, $r_textsearch, $searchfields_ordering, $inputbox_style);

//#####END SEARCH POST#####



//#####LIST WITHDRAWAL#####

unset($dis);

if($_GET["_q"]=="pending" && $_POST["__req"]){

	foreach($post_s as $f=>$v){

		if(preg_match('/^_cpay_[0-9]+$/', $f) || preg_match('/^payment_date_[0-9]+$/', $f) || preg_match('/^payment_remark_[0-9]+$/', $f)){

			$dis[$f]=$post_h[$f];

		}

	}

}




if($searching){

	$count_sql=$search_query["count_sql"];

	$record_sql=$search_query["record_sql"];

}else{

	$count_sql="select count(*) from $db->incentive where uid>0";

	$record_sql="select * from $db->incentive where uid>0 order by id limit <%limit_start%>, <%limit%>";

}



$cur_page=$_GET["nav"];

$total_record=@mysql_result(mysql_query($count_sql), 0);

if($_GET["_q"]=="pending"){

	$this_page_total=@mysql_num_rows($r=mysql_query($record_sql));

}else{

	$total_page=ceil($total_record/$item_per_page);

	$page=($cur_page? ($cur_page>$total_page? $total_page : ($cur_page<1? 1 : $cur_page)) : 1);

	$limit_start=($page-1)*$item_per_page;

	$nav_link=format_page_admin($page, $total_page, $this_file);

	$this_page_total=@mysql_num_rows($r=mysql_query(str_replace("<%limit_start%>", $limit_start, str_replace("<%limit%>", $item_per_page, $record_sql))));

}

$k=0;

for($i=0;$i<$this_page_total;$i++){

	$wit=mysql_fetch_assoc($r);

	if($wit["uid"] && !$r_user[$wit["uid"]]){

		$r_user[$wit["uid"]]=get_user_detail_by_id($wit['uid']);

	}

	$wit_row.="

	<tr class='row$k'>

		<td>$wit[id]</td>

		<td>".$r_user[$wit["uid"]]["username"]."</td>

		<td>$wit[package]</td>

		<td>$wit[counter]</td>

		<td>".number_format($wit["amount"], 2)."</td>

		<td>".number_format($wit["payout"], 2)."</td>

		<td>".number_format($wit["total_bonus"], 2)."</td>

		<td>".number_format(($wit["payout"]/$wit["total_bonus"])*100, 2)."%</td>

	</tr>";

	$k=1-$k;

}



//listing text
$listing_text=replace_tag($searching? __("Search returned <%x%> records.") : __("Listing <%x%> records."), array("<%x%>"=>strval($total_record))).($searching? " ".replace_tag(__("<%link%>Clear Search and Refresh<%/link%>."), array("<%link%>"=>"<a href='$this_file'>", "<%/link%>"=>"</a>")) : "");



//listing

$wit_list=($msg || $errmsg ? $msg.$errmsg : "").

"
$search_ui<br />\n

$listing_text $nav_link

<table class='aml_table' style='width:1200px;'>

	<tr class='aml_header'>

		<td>".__("ID")."</td>

		<td>".__("Member ID")."</td>

		<td>".__("Package")."</td>

		<td>".__("Counter")."</td>

		<td>".__("ROI")."</td>

		<td>".__("Payout")."</td>

		<td>".__("Target Payout")."</td>

		<td>".__("Percentage")."</td>

	</tr>

    $wit_row

</table>

</form>

$nav_link";

//#####END WITHDRAWAL LIST#####



$content="<h2>$page_title</h2>$wit_list";



print format_admin_page($content, $this_title);



function wit_search($in_get){

	global $vars, $pv;

	$db=$vars["db"];



	$get=$in_get;

	$search_type="and";

	$r_like_search=array("acc_name","acc_payee_name","acc_no");

	//format search string

	foreach($get as $field=>$value){

		$value=trim($value);

		//search checkbox option

		if(preg_match('/^_so_/', $field)){

			preg_match('/(?<=_so_).+(?=__)/', $field, $matchf);

			$f=$matchf[0];

			preg_match('/(?<=__).+$/', $field, $matchv);

			$v=$matchv[0];

			$r_searchquery[$f].=($r_searchquery[$f]? "," : "")."'$v'";

		}

		//db field name search

		if(substr($field, 0, strlen("_sp_"))=="_sp_"){

			if(strlen($value)){

				$db_fieldname="a.".substr($field, strlen("_sp_"));
				
				if($db_fieldname=="a.uid") $db_fieldname="b.username";
				
				$db_fieldname_q.=($db_fieldname_q? " and " : "")."(".(in_array($db_fieldname, $r_like_search)? format_search_field_like($db_fieldname, $value) : format_search_field($db_fieldname, $value)).")";

			}

		}

	}

	if(isset($r_searchquery)){

		foreach($r_searchquery as $field => $query){

			$option_q.=($option_q? " and " : "")."a.$field in ($query)";

		}

	}else{

		$option_q="1=1";

	}

	$db_fieldname_q=$db_fieldname_q? $db_fieldname_q : "1=1";



	$count_sql="select count(a.*) from $db->incentive a inner join $db->users b on b.id=a.uid where $option_q and $db_fieldname_q";

	$record_sql="select a.*,b.username from $db->incentive a inner join $db->users b on b.id=a.uid where $option_q and $db_fieldname_q order by a.id limit <%limit_start%>, <%limit%>";

	return array("count_sql"=>$count_sql, "record_sql"=>$record_sql);

}

?>